import {
	defineStore
} from 'pinia';
import {
	strFormatMinutesColumn
} from '@/utils/format.js'

export const useRecordingStore = defineStore('recording', {
	state: () => ({
		isRecording: false, // 管理全局录音状态
		isPaused: false, // 是否暂停
		recorderData: null, // 保存录音数据
		audioBlob: null, // 保存录音的 Blob 数据
		duration: 0, // 录音时长
		recorderInstance: null, // 录音实例
		upName: null
	}),
	actions: {
		startRecording(instance) {
			this.isRecording = true;
			this.isPaused = false;
			this.recorderInstance = instance; // 记录录音实例
			this.upName = `${strFormatMinutesColumn(Date.now())} 录音.wav`;
		},
		stopRecording(audioData, duration) {
			this.isRecording = false;
			this.isPaused = false;
			this.recorderData = audioData;
			this.duration = duration;
			this.recorderInstance = null;
		},
		pauseRecording(audioData, duration) {
			this.isRecording = true;
			this.isPaused = true;
			this.recorderData = audioData;
			this.duration = duration;
		},
		resumeRecording() {
			this.isRecording = true;
			this.isPaused = false;
		},
		cancelRecording(){
			this.isRecording = false;
			this.isPaused = false;
			this.recorderData = null;
			this.duration = null;
			this.recorderInstance = null;
		},
		setAudio(blob) {
			this.audioBlob = blob;
		},
		toggleRecording() {
			this.isRecording = !this.isRecording;
		},
	},
});